import { observable, action, autorun, toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';
import {WeaForm, WeaTableNew} from 'comsMobx'
import {message, Button } from "antd";
import * as Apis from '../../apis/info';
import {Text} from '../../components/util/text';
const text = new Text();

const {TableStore} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
export class infoDialogStore {

    @observable form = new WeaForm();    //新建上报信息弹出框表单
    @observable condition = [];    //新建上报弹出框condition

    @observable status = {
        visible: false,     //待报信息dialog的visible
    }

    pathIds = [];

    parent = null;
    constructor(parent) {
        this.parent = parent;
    }

    setState = (params = {}) => {
        Object.keys(params).forEach(o => {
            this.status[o] = params[o];
        })
        return this;
    }

    //初始化弹出框condition
    @action
    initDialogForm = (params = {}) => {
        this.form = new WeaForm();
        this.condition = [];
        Apis.getInfoCondition(params).then(data => {
            if (data.api_status == '1') {
                if (!data.isRight) {
                    message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                    return;
                }
                this.condition = data.infoConditionGroup;
                this.pathIds = data.pathIds;
                this.form && !this.form.isFormInit && this.form.initFormFields(this.condition);
                this.setState({ visible: true });
            } else {
                message.error(data.api_errormsg);
            }
        });
    }

    //保存弹框中新建的待报信息
    saveReportForm = () => {
        let formParams = this.form.getFormParams();
        let params = {...formParams, 'pathIds': this.pathIds };
        let checkPath = false;
        //判断当前用户是否已选择报送单位
        for (var i in params) {
            this.pathIds.map(c => {
                if (i == c) {
                    if (params[i][0] == "1") {
                        checkPath = true;
                    }
                }
            });
        }
        if (!checkPath) {
            message.warn(text.TIP1);
            return;
        }
        if (params.reportType == '1' && params.atach == '') {
            message.warn(text.TIP2);
            return;
        }
        this.form.validateForm().then(f => {
            if (f.isValid) {
                Apis.saveInfoReport(params).then(data => {
                    if (data.api_status == '1') {
                        if (!data.isRight) {
                            message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                            return;
                        }
                        message.success(text.SAVESUCCESS);
                    } else {
                        message.error(data.api_errormsg);
                    }
                    this.setState({ visible: false });
                    this.parent.reLoad();    //父界面table刷新
                    // this.getInfoReportList({type:'waitReport'});
                });
            } else {
                f.showErrors();
            }
        });
    }

}